import { http } from '@/utils/http'
import type {
  DeliverymanInfoDTO,
  DriverFaceModelForm,
  UpdateDriverLocationForm,
  DriverSetDTO,
  NewOrderDataVo,
  DeliveryTasksDTO,
  CalculateDrivingLineForm,
  DrivingLineVo,
  locationDataform,
  endDriverForm,
} from '@/types/home'

/**
 * 获取认证信息
 * @param data 请求参数
 */
export const authInfoAPI = (id: string) => {
  return http<DeliverymanInfoDTO>({
    method: 'GET',
    url: '/sys/app/deliverymaninfo/' + id,
    data: {},
  })
}
export const updateDriverAuthInfoAPI = (data: DeliverymanInfoDTO) => {
  return http<string>({
    method: 'POST',
    url: '/sys/app/deliverymaninfo/updateDriverAuthInfo',
    data,
  })
}
export const createDriverFaceModelAPI = (data: DriverFaceModelForm) => {
  return http({
    method: 'POST',
    url: '/sys/app/deliverymaninfo/creatDriverFaceModel',
    data,
  })
}
export const verifyDriverFaceAPI = (data: DriverFaceModelForm) => {
  return http<boolean>({
    method: 'POST',
    url: '/sys/app/deliverymaninfo/verifyDriverFace',
    data,
  })
}

export const updateDriverLocationAPI = (data: UpdateDriverLocationForm) => {
  return http<boolean>({
    method: 'POST',
    url: `/sys/app/driverorders/updateDriverLocation`,
    data,
  })
}

export const startServiceAPI = (driverId: string) => {
  return http<boolean>({
    method: 'GET',
    url: `/sys/app/driverorders/startService`,
    data: {
      driverId,
    },
  })
}
export const stopServiceAPI = (driverId: string) => {
  return http<boolean>({
    method: 'GET',
    url: `/sys/app/driverorders/stopService`,
    data: {
      driverId,
    },
  })
}

export const getDriverSetAPI = (driverId: string) => {
  return http<DriverSetDTO>({
    method: 'GET',
    url: `/sys/app/driverset/getDriverSet/${driverId}`,
    data: {},
  })
}
export const saveOrUpdateAPI = (data: DriverSetDTO) => {
  return http<DriverSetDTO>({
    method: 'POST',
    url: `/sys/app/driverset/saveOrUpdate`,
    data,
  })
}

/**
 *
 * @param data 查询司机新订单数据
 * @returns
 */
export const findNewOrderQueueDataAPI = (driverId: string) => {
  return http<NewOrderDataVo[]>({
    method: 'GET',
    url: `/sys/app/driverorders/findNewOrderQueueData/${driverId}`,
    data: {},
  })
}
/**
 * 抢单
 * @param driverId
 * @param orderId
 * @returns
 */
export const robNewOrderAPI = (driverId: string, orderId: string) => {
  return http<boolean>({
    method: 'GET',
    url: `/sys/app/driverorders/robNewOrder/${driverId}/${orderId}`,
    data: {},
  })
}

/**
 * 司机端查找当前订单
 */
export const searchDriverCurrentOrderAPI = (driverId: string) => {
  return http<DeliveryTasksDTO>({
    method: 'GET',
    url: `/sys/app/driverorders/searchDriverCurrentOrder/${driverId}`,
    data: {},
  })
}

/**
 * 计算驾驶路线
 * @param driverId
 * @returns
 */
export const calculateDrivingLineAPI = (data: CalculateDrivingLineForm) => {
  return http<DrivingLineVo>({
    method: 'POST',
    url: `/sys/app/map/calculateDrivingLine`,
    data,
  })
}

interface StartDriveForm {
  orderId: number
  driverId: number
  userId: number | string
}
/**
 * 开始服务
 * @param data
 * @returns
 */
export const startDriveAPI = (data: StartDriveForm) => {
  return http<boolean>({
    method: 'POST',
    url: `/sys/app/driverorders/startDriver`,
    data,
  })
}

interface updateOrderLocationToCacheForm {
  orderId: number
  latitude: number
  longitude: number
}
export const updateOrderLocationToCacheAPI = (data: updateOrderLocationToCacheForm) => {
  return http<boolean>({
    method: 'POST',
    url: `/sys/app/driverorders/updateOrderLocationToCache`,
    data,
  })
}

/**
 * 司机到达起始点并取货
 */
export const driverArriveStartLocationAPI = (orderId: number | null, driverId: number | null) => {
  return http<boolean>({
    method: 'GET',
    url: `/sys/app/driverorders/driverArriveStartLocation/${orderId}/${driverId}`,
    data: {},
  })
}
export const updateDriverOrderLocationToCacheAPI = (data: locationDataform[]) => {
  return http<boolean>({
    method: 'POST',
    url: `/sys/app/driverorders/saveOrderServiceLocation`,
    data,
  })
}
export const endDriverAPI = (data: endDriverForm[]) => {
  return http<boolean>({
    method: 'POST',
    url: `/sys/app/driverorders/endDriver`,
    data,
  })
}
